<?php

class AdminModel extends Model {

    /**
     * 用户登录
     * @param type $username
     * @param type $password
     * @return boolean
     */
    public function login($username, $password) {
        $row = $this->query("SELECT * FROM s_admin WHERE username = '{$username}'", FALSE);        
        //判断用户名是否存在
        if (empty($row)) {
            return false;
        }
        //判断密码MD5值是否正确
        if ($row['password'] == md5($password)) {
            session_start();
            $_SESSION['token'] = 'loggedin';
            return true;
        }
    }

    /**
     * 判断管理员登录状态
     */
    public function identifyLoggedin() {
        session_start();
        if (empty($_SESSION['token'])) {
            header("Location:admin.php?action=login");
            die();
        }
        if ($_SESSION['token'] != 'loggedin') {
            header("Location:admin.php?action=login");
            die();
        }
    }

}
